/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 RemoteSmartObject.hpp. Created on: 18.11.2009 15:15:20 
 */

#ifndef REMOTESMARTOBJECT_HPP_
#define REMOTESMARTOBJECT_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"
#include "smartobjects/IRemoteSmartObject.hpp"
#include "smartobjects/core/SmartObject.hpp"
#include "smartobjects/core/ICommandProcessor.hpp"
#include "smartobjects/network/INetworkCommand.hpp"
#include "smartobjects/network/ConnectionHandler.hpp"
#include "smartobjects/network/IManagedConnectionListener.hpp"
#include "smartobjects/network/ProxyManagedConnection.hpp"
#include "smartobjects/coders/CallInterfaceOperationRequest.hpp"
#include "smartobjects/coders/CallInterfaceOperationResponse.hpp"


namespace smartobjects {

class RemoteSmartObject :
	public virtual IRemoteSmartObject,
	public ICommandProcessorHandler,
	public virtual SmartObject,
	public IManagedConnectionListener {
public:
	SO_DLLPRFX RemoteSmartObject();
	SO_DLLPRFX RemoteSmartObject(const std::string& sInstanceId);
	SO_DLLPRFX virtual ~RemoteSmartObject();

	SO_DLLPRFX virtual void resolve( ISmartBus* poBus, IEndpointLocation* poLocation);
	SO_DLLPRFX virtual void resolve( IRemoteSmartObject* poBaseClient);
	SO_DLLPRFX virtual void resolve( ISmartObject* poBaseObject, ConnectionHandler* poConnectionHandler );

	SO_DLLPRFX virtual void setDefaultRequestContext(RequestContext* poContext);
	SO_DLLPRFX virtual RequestContext* getDefaultRequestContext();

	SO_DLLPRFX virtual void addAvailabilityListener(IRemoteSmartObjectAvailabilityListener* poListener);
	SO_DLLPRFX virtual void delAvailabilityListener(IRemoteSmartObjectAvailabilityListener* poListener);

	SO_DLLPRFX virtual bool isAvailable();
	SO_DLLPRFX virtual CallInterfaceOperationRequest* createCallRequest(const std::string& sOperationId, RequestContext* poRequestContext);
	SO_DLLPRFX virtual ConnectionHandler* getConnectionHandlerToSend();
	SO_DLLPRFX virtual void callAsync(ConnectionHandler* poConnectionHandler, CallInterfaceOperationRequest* poRequest, RefObject* poUserObject);
	SO_DLLPRFX virtual CallInterfaceOperationResponse* call(ConnectionHandler* poConnectionHandler, CallInterfaceOperationRequest* poRequest);

protected:
	SO_DLLPRFX virtual void onCallResponseReceived(
			ConnectionHandler* poConnectionHandler,
			INetworkCommand* poRequest,
			CallInterfaceOperationResponse* poCallResponse,
			RefObject* poUserObject) = 0;

public:
	// Constants
	static const unsigned int COMMAND_BUF_INITIAL_CAPACITY;
	static const unsigned int COMMAND_BUF_MAX_CAPACITY;

protected:
	// ICommandProcessorHandler
	SO_DLLPRFX virtual void onResponseReceived(
			ConnectionHandler* poHandler,
			INetworkCommand* poRequest,
			INetworkCommand* poResponse,
			RefObject* poUserObject
	);

	SO_DLLPRFX virtual void onRequestReceived(
			ConnectionHandler* poHandler,
			INetworkCommand* poRequest
	);

	SO_DLLPRFX virtual bool isCommandIdSupported(
			ConnectionHandler* poHandler,
			INetworkCommand* poRequest
	);

protected:
	// IManagedConnectionListener
	SO_DLLPRFX virtual void onConnected(IManagedConnection*) throw();
	SO_DLLPRFX virtual void onConnected(ConnectionHandler*) throw();

	SO_DLLPRFX virtual void onDisconnected(IManagedConnection*) throw();
	SO_DLLPRFX virtual void onDisconnected(ConnectionHandler*) throw();

	SO_DLLPRFX virtual void onConnectionFailure(IManagedConnection* poManagedConnection, const std::string& sErrorMessage) throw();
	SO_DLLPRFX virtual void onConnectionFailure(ConnectionHandler* poHandler, const std::string& sErrorMessage) throw();

protected:
	SO_DLLPRFX virtual void fireOnAvailable();
	SO_DLLPRFX virtual void fireOnUnavailable();
	SO_DLLPRFX virtual void processingPostActionAvailabilityListeners();
	SO_DLLPRFX virtual void releaseCurrentNetworkEnv();
	SO_DLLPRFX virtual void releaseBuffersPool();
	SO_DLLPRFX virtual void returnBufferToPool(INetworkCommand* poCommand);

	SO_DLLPRFX virtual unsigned int getTimeoutInSec(CallInterfaceOperationRequest*);
	SO_DLLPRFX virtual ICommandProcessor* getCurrentCommandProcessor();

protected:
	boost::recursive_mutex m_mxRemoteObject;

	typedef std::list<IRemoteSmartObjectAvailabilityListener*> AvailabilityListeners;
	AvailabilityListeners m_aAvailabilityListeners;
	PostActionListenersContainer<IRemoteSmartObjectAvailabilityListener> m_oPALAvailabilityListeners;
	boost::recursive_mutex m_mxAvailabilityListeners;

	bool m_bAvailableStatus;
	RequestContext* m_poDefaultRequestContext;
	IManagedConnection* m_poManagedConnection;

	typedef std::list<ByteBuffer*> BuffersPool;
	BuffersPool m_aBuffersPool;
	boost::recursive_mutex m_mxBuffersPool;
};

}

#endif /* REMOTESMARTOBJECT_HPP_ */
